<?php

namespace app\adminapi\controller;

use app\helpers\Common;

use think\facade\Db;
use think\Exception;
use think\Request;

class File extends Base
{
    public function initialize()
    {
        parent::initialize();

        $this->modelName = 'File';
    }

    /**
     * List
     */
    public function list(Request $request)
    {
        $param = $request->param();

        $page = 1;
        $limit = 20;
        if (!empty($param['page'])) {
            $page = (int)$param['page'];
        }
        if (!empty($param['limit'])) {
            $limit = (int)$param['limit'];
        }

        $className = '\\app\\' . $this->appName . '\\model\\' . $this->modelName;
        $model = new $className;
        $field = $this->defaultField;
        $where = $this->getFilterWhere($request);
        if ($this->defaultWhere) {
            $where = array_merge($this->defaultWhere, $where);
        }
        if ($this->defaultOrder) {
            $order = $this->defaultOrder;
        }

//        $data = $model->where($where)->field($field)->order($order)->limit($page, $limit)->select();
//        $count = $model->where($where)->count();

        $data = $model->where($where)->field($field)->order($order)->paginate(['page' => $page, 'list_rows' => $limit]);
        $data = json_decode(json_encode($data), true);
        $list = &$data['data'];
        if ($list) {
            // 处理列表数据
            $list = $this->handleListData($list);
        }

        // 文件夹
        $data['folders'] = [
            'article', 'file', 'images', 'default'
        ];
        // 文件类型
        $data['file_types'] = [
            'image' => '图片',
            'video' => '视频',
            'audio' => '音频',
            'word' => '文档',
            'other' => '其它',
        ];
        // 存储方式
        $data['storages'] = config('option.storages');
        $data['ids'] = array_column($list, 'id');

        return api_response(self::STATUS_SUCCESS, '', $data);
    }

    /**
     * 分类
     */
    public function classify(Request $request)
    {
        $className = '\\app\\' . $this->appName . '\\model\\' . $this->modelName;
        $model = new $className;

        // 文件夹
        $folders = $model->group('folder')->column('folder');
        // 文件类型
        $file_types = [
            [
                'id' => 'image',
                'name' => '图片'
            ],
            [
                'id' => 'video',
                'name' => '视频'
            ],
            [
                'id' => 'audio',
                'name' => '音频'
            ],
            [
                'id' => 'word',
                'name' => '文档'
            ],
            [
                'id' => 'other',
                'name' => '其它'
            ]
        ];

        $data = [
            'folders' => $folders,
            'file_types' => $file_types,
        ];

        return api_response(self::STATUS_SUCCESS, '', $data);
    }

    /**
     * 处理列表数据
     */
    protected function handleListData($data)
    {
        $className = '\\app\\' . $this->appName . '\\model\\' . $this->modelName;
        $model = new $className;

        $data = $model->formatDatas($data, ['format_source' => 'backend']);

        return $data;
    }

    /**
     * 筛选条件
     */
    protected function getFilterWhere($request)
    {
        $param = $request->param();
        $where = [];
        if (isset($param['folder']) && $param['folder']) {
            $where[] = ['folder', '=', $param['folder']];
        }
        if (isset($param['file_type']) && $param['file_type']) {
            $where[] = ['file_type', '=', $param['file_type']];
        }
        if (isset($param['range_date']) && $param['range_date']) {
            $rangeDate = $param['range_date'];
            $startTime = strtotime($rangeDate[0]);
            $endTime = strtotime($rangeDate[1]);
            $where[] = ['created_at', '>=', $startTime];
            $where[] = ['created_at', '<=', $endTime];
        }

        return $where;
    }

    protected function afterDelete($data)
    {
        $className = '\\app\\' . $this->appName . '\\model\\' . $this->modelName;
        $model = new $className;
        $delFileRes = $model->delFile($data);
        if ($delFileRes['code'] != 1) {
            return ['code' => 0, 'msg' => $delFileRes['msg']];
        }

        return parent::afterDelete($data);
    }
}
